package com.zhuiyun.project.config.MybatisPlus;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author xy
* @Description todo mybatis-plus配置
* @Date 2023/12/1 15:57
* @Return
* @param
*/
@Configuration
@EnableConfigurationProperties(TenantProperties.class)
@MapperScan(value = "com.zhuiyun.project.**.mapper")
public class MybatisPlugConfig {

    private final TenantProperties tenantProperties;

    public MybatisPlugConfig(TenantProperties tenantProperties) {
        this.tenantProperties = tenantProperties;
    }

    /*
    * @Author xy
    * @Description todo 自定义多重规则租户校验
    * @Date 2023/12/1 15:56
    * @Return
    * @param
    */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        if (tenantProperties.getEnable()) {
            // 加载自定义租户实现逻辑
            interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantMultiHandler(tenantProperties)));
        }
//        // 分页插件
//        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
//        // 乐观锁插件
//        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
//        // 阻断插件
//        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        return interceptor;
    }


    /*
    * @Author xy
    * @Description todo 分页插件
    * @Date 2023/12/1 15:56
    * @Return
    * @param
    */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }



}
